﻿using EventNext.Events;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace BeetleX.XRPC.HostedServices;


public class XRPCServerHosting : IHostedService
{
    //private ServiceCollection mXRPCControllerServices = new ServiceCollection();
    //private IServiceProvider mXRPCControllerServiceProvider;

    private IServiceProvider ServicesProvider;

    private HttpSettingHandler mSettingHandler;

    private XRPCServer xRPCServer;

    public XRPCServerHosting(HttpSettingHandler httpSettingHandler, IServiceProvider servicesProvider)
    {
        mSettingHandler = httpSettingHandler;
        ServicesProvider = servicesProvider;
    }

    private void InitService()
    {
        xRPCServer["__ServiceProvider"] = this.ServicesProvider;
        if (mSettingHandler.Assemblies != null)
        {
            xRPCServer.Register(mSettingHandler.Assemblies);
        }
    }

    public virtual Task StartAsync(CancellationToken cancellationToken)
    {
        xRPCServer = new XRPCServer();
        mSettingHandler.Options(xRPCServer);
        xRPCServer.EventCenter.ServiceInstance += delegate (object o, EventServiceInstanceArgs e)
        {
            e.Service = ServicesProvider.GetService(e.Type);
        };
        InitService();
        xRPCServer.Open();
        mSettingHandler.StartCallBack?.Invoke(xRPCServer);
        return Task.CompletedTask;
    }

    public virtual Task StopAsync(CancellationToken cancellationToken)
    {
        xRPCServer.Dispose();
        return Task.CompletedTask;
    }

}


//using System;
//using System.Reflection;
//using System.Threading;
//using System.Threading.Tasks;
//using EventNext;
//using EventNext.Events;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Hosting;

//namespace BeetleX.XRPC.HostedServices;


//public class XRPCServerHosting : IHostedService
//{
//    private ServiceCollection mXRPCControllerServices = new ServiceCollection();

//    private IServiceProvider mXRPCControllerServiceProvider;

//    private HttpSettingHandler mSettingHandler;

//    private XRPCServer xRPCServer;

//    public XRPCServerHosting(HttpSettingHandler httpSettingHandler)
//    {
//        mSettingHandler = httpSettingHandler;
//    }

//    private void InitService()
//    {
//        ServiceDescriptor[] array = new ServiceDescriptor[mSettingHandler.Services.Count];
//        mSettingHandler.Services.CopyTo(array, 0);
//        ServiceDescriptor[] array2 = array;
//        foreach (ServiceDescriptor item in array2)
//        {
//            mXRPCControllerServices.Insert(0, item);
//        }

//        Assembly[] assemblies = mSettingHandler.Assemblies;
//        for (int i = 0; i < assemblies.Length; i++)
//        {
//            Type[] types = assemblies[i].GetTypes();
//            foreach (Type type in types)
//            {
//                if (type.GetCustomAttribute<ServiceAttribute>(inherit: false) != null)
//                {
//                    mXRPCControllerServices.AddTransient(type);
//                }
//            }
//        }

//        mXRPCControllerServices.AddSingleton(xRPCServer);
//        mXRPCControllerServiceProvider = mXRPCControllerServices.BuildServiceProvider();
//        xRPCServer["__ServiceProvider"] = mXRPCControllerServiceProvider;
//        if (mSettingHandler.Assemblies != null)
//        {
//            xRPCServer.Register(mSettingHandler.Assemblies);
//        }
//    }

//    public virtual Task StartAsync(CancellationToken cancellationToken)
//    {
//        xRPCServer = new XRPCServer();
//        mSettingHandler.Options(xRPCServer);
//        xRPCServer.EventCenter.ServiceInstance += delegate (object o, EventServiceInstanceArgs e)
//        {
//            e.Service = mXRPCControllerServiceProvider.GetService(e.Type);
//        };
//        InitService();
//        xRPCServer.Open();
//        mSettingHandler.StartCallBack?.Invoke(xRPCServer);
//        return Task.CompletedTask;
//    }

//    public virtual Task StopAsync(CancellationToken cancellationToken)
//    {
//        mXRPCControllerServices.Clear();
//        xRPCServer.Dispose();
//        return Task.CompletedTask;
//    }
//}
